package com.cow.sram.module.distribution;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cow.framework.utils.reflection.MapToBeanUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.data.entity.Resourceinfo;
import com.cow.sram.data.entity.Roleresource;
import com.cow.sram.data.entity.Userrole;
import com.cow.sram.module.managerResource.ResourceinfoService;

/**
 * 角色资源
 * @author Administrator
 *
 */
@RestController
@EnableAutoConfiguration
@Transactional
@RequestMapping(value="/distribution")
public class RoleResourceController extends BaseComponent{

	@Autowired
	RoleResourceService roleResourceService;
	@Autowired
	ResourceinfoService resourceinfoService;
	
	/**查询角色资源列表*/
	@RequestMapping(value="/findRoleresource")
	public Object findRoleresource(@RequestBody Map parmMap){
		parmMap.put("userid", super.getCurUser().getUserid());
		List<Resourceinfo> resourceList = roleResourceService.findUserResource(parmMap);
		for(Resourceinfo info:resourceList){
			//判断该角色是否已分配此资源权限
			if(roleResourceService.isExist((Integer)parmMap.get("roleid"), info.getResourceid())){
				info.setGrantFlag(1);
			}else{
				info.setGrantFlag(0);
			}			
		}
		
		//判断是否要获取树形资源
		if(parmMap.get("resourcetype").toString().startsWith("t")){
			resourceList = resourceinfoService.listToTree(resourceList);
		}
		
		return resourceList;
	}
	
	
	/**保存*/
	@RequestMapping(value="/setRoleResource")
	public Object setRoleResource(@RequestBody Map parmMap){
		//重新授权角色
		String resourceIds = (String)parmMap.get("resourceIds");
		Integer roleId=Integer.valueOf(parmMap.get("roleId").toString());
		String resourcetype=(String)parmMap.get("resourcetype");
		String systemid=(String)parmMap.get("systemid");
		//删除用户原有角色
		roleResourceService.delRoleResourceByroleId(roleId, resourcetype, systemid);
		if(resourceIds!=null){
			String[] resourceArray = resourceIds.split(",");
			for(String resourceId:resourceArray){
				Roleresource rs=new Roleresource();
				rs.setRoleid(roleId);
				rs.setResourceid(Integer.valueOf(resourceId));
				rs.setPrivilegetype(3);;
				roleResourceService.newRoleresource(rs);
		}
		}
		     return "success";
		
	}
	
		
	/**根据ID查看角色资源*/
	@RequestMapping(value="/getRoleresourceById")
	public Object getRoleresourceById(@RequestParam Integer id){
		return roleResourceService.getRoleresourceById(id);
	}
	
	/**新增记录*/
	@RequestMapping(value="/saveRoleresource")
	public Object saveRoleresource(@RequestBody Roleresource rsc){
		 roleResourceService.newRoleresource(rsc);
		 return "success";
	}
	
	/**编辑*/
	@RequestMapping(value="/editRoleresource")
	public Object editRoleresource(@RequestBody Map parmMap){
		//根据Id查询角色资源
		Roleresource rsc=roleResourceService.getRoleresourceById(Integer.valueOf(parmMap.get("mapid").toString()));
		MapToBeanUtil.copyMapToBean(parmMap, rsc);  //拷贝到实体
		roleResourceService.newRoleresource(rsc);
		return "success";
		
	}
	
	/**删除角色资源*/
	@RequestMapping(value="/delRoleresource")
	public Object delRoleresource(@RequestParam Integer id){
		roleResourceService.delRoleresource(id);
		return "success";
	}
}
